﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace Denting
{
    public class Nodes:List<Node>
    {
        private int _nodeID;
        private bool FindByNodeID(Node n)
        {
            if (n.NodeID == _nodeID)
            {
                return true;
            }
            else
                return false;
        }
        public Nodes GetNodesFromInputFile(string inpFullFileName)
        {
            string tempInputFile = inpFullFileName + ".david";
            this.Clear();
            Debug.WriteLine("************************************{0}", inpFullFileName);
            File.Copy(inpFullFileName, tempInputFile);
            StreamReader sr = new StreamReader(tempInputFile);

            string s = sr.ReadLine();
            string[] sArray;
            bool canReadNode=false;
            while (s != null)
            {
                if (canReadNode)
                {
                    if (s.Length>0 &&s[0]=='*')
                    {
                        canReadNode = false;
                        break;
                    }
                    sArray = s.Split(',');
                    Node n = new Node(Convert.ToInt32(sArray[0]), Convert.ToDouble(sArray[1]), Convert.ToDouble(sArray[2]), Convert.ToDouble(sArray[3]));
                    this.Add(n);
                }
                if (s == "*NODE")
                {
                    canReadNode = true;
                }
                s = sr.ReadLine();
            }
            sr.Close();
            File.Delete(tempInputFile);
            return this;

        }

        public Nodes FindNodesAttatchToElements(Elements es)
        {
            //这个是原始算法，有点慢，执行时间为12秒

            /*Nodes tempNodes = new Nodes();
            foreach (Element e in es)
            {
                foreach (Node n in this)
                {
                    if (e.Node_1==n.NodeID||e.Node_2==n.NodeID||e.Node_3==n.NodeID||e.Node_4==n.NodeID)
                    {
                        if (!tempNodes.ContainsNodeID(n.NodeID))
                        {
                            tempNodes.Add(n);
                        }
                    }
                }
            }
            return tempNodes;*/

            /////////////////////////////////////////////////////////////////


            //这个算法用时1.7秒
            HashSet<int> setNodeID = new HashSet<int>();
            Nodes tempNodes = new Nodes();

            Stopwatch w = new Stopwatch();
            w.Start();
            foreach (Element e in es)
            {
                setNodeID.Add(e.Node_1);
                setNodeID.Add(e.Node_2);
                setNodeID.Add(e.Node_3);
                if (!e.IsElementTriangle)
                {
                    setNodeID.Add(e.Node_4);
                }
            }
            Debug.WriteLine("********************************************{0}", w.Elapsed);
            foreach (int i in setNodeID)
            {
                _nodeID = i;
                tempNodes.Add(this.Find(FindByNodeID));
            }
            Debug.WriteLine("********************************************{0}", w.Elapsed);
            return tempNodes;
            

        }

        public bool ContainsNodeID(int nodeID)
        {
            foreach (Node n in this)
            {
                if (n.NodeID==nodeID)
                {
                    return true;
                }
            }
            return false;
        }

        public Nodes CombinNodes(Nodes ns)
        {
            foreach (Node n in ns)
            {
                if (!this.ContainsNodeID(n.NodeID))
                {
                    this.Add(n);
                }
            }
            return this;
        }

        public Nodes ExceptNodes(Nodes ns)
        {
            for (int i = this.Count - 1; i >= 0;i-- )
            {
                if (ns.ContainsNodeID(this[i].NodeID))
                {
                    this.RemoveAt(i);
                }
            }
            return this;
        }

        public Node GetNodeFromNodeID(int nodeID)
        {
            //这个耗时1.3秒

            _nodeID = nodeID;
            return this.Find(FindByNodeID);


            //这个耗时还是比较长的1.7秒
            /*Node tempNode = new Node();
            foreach (Node n in this)
            {
                if (n.NodeID==nodeID)
                {
                    tempNode = n;
                    break;
                }
            }
            return tempNode;*/
        }

        public Nodes CloneFromNodes(Nodes ns)
        {
            this.Clear();
            foreach (Node n in ns)
            {
                this.Add(n.Clone());
            }
            return this;
        }
    }
}
